import axios from 'axios';
import {url} from "@/assets/js/config";
import Vue from 'vue';
import store from '../../vue/vuex/store'
import router from '@/vue/router/';

let instance = axios.create({
    timeout: 10000,
    baseURL: process.env.NODE_ENV === "development" ? "" : "",
    withCredentials: true // 允许携带cookie
});
// Toast.allowMultiple();
instance.interceptors.request.use((config) => {
    if (config.method === 'post' || config.method === 'put') {
        config.headers["Content-Type"] = "application/json;";
        
        let sessionId = localStorage.getItem('sessionId');

        if(sessionId){
            config.headers["x-auth-token"] = sessionId
        }
        
        config.headers["channel"] = "app";
       
        config.data = JSON.stringify(config.data);
    }
    // if (!config.headers.Authorization) {
    //     config.headers.Authorization = 'Bearer ' + (window.localStorage.getItem('Authorization') || '');
    // }
    return config;
}, (err) => {
    return Promise.reject(err);
});

instance.interceptors.response.use((res) => {
    //code=2   0元支付
    if (res.data.code != 0 && res.data.code != 2) {
        if (res.data.code == 700) { //当前用户未登录
            Vue.prototype.$message.error("登录已过期 请重新登录!");

            setTimeout(() => {
                router.replace('/login')
            }, 1000);
        } else {
            switch (res.data.code) {
                case -1:
                    break;
                case 100:
                    break;
                case 101:
                    break;
                case 999:
                    break;
            }
            if (res.data.description) {
             
            } else {
          
            }
        }
        return Promise.reject(res);
    }
    return res;
}, (error) => {
    if (error.message == 'timeout of 5000ms exceeded') {
        return Promise.reject(error);
    }
    // if (!(error.code == 700 && error.message.indexOf('微信授权登录验证不通过') >= 0)) {
    //     Dialog.alert({
    //         title: '警告',
    //         message: error.message
    //     })
    // }
    return Promise.reject(error);
});

const post = (reqUrl, data, config = {},authToken) => {
    reqUrl = url().baseUrl +  reqUrl

    // let appId = store.state.appId
    // data.appId = appId

    return instance.post(reqUrl, data, config , authToken)
}

const put = (url, data, config = {}) => {
    return instance.put(url, data, config)
}

const get = (url, params, config = {}) => {
    reqUrl = url().baseUrl +  reqUrl
    return instance.get(reqUrl, {
        params: params,
        ...config
    })
}

const deleteMethod = (url, config = {}) => {
    return instance({
        url: url,
        method: 'delete',
        ...config
    })
}

export default {
    install(Vue) {
        Object.defineProperties(Vue.prototype, {
            $reqGet: {
                value: get
            },
            $reqPost: {
                value: post
            },
            $reqPut: {
                value: put
            },
            $reqDel: {
                value: deleteMethod
            },
        })
    }
}